package com.jianyun.jyzs.dao;

import android.content.Context;
import android.text.TextUtils;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.jianyun.jyzs.bean.SafeInsResponse;
import com.jianyun.jyzs.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.rongcloud.rce.lib.CacheTask;
import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by mlli on 2017/9/20.
 */

public class SecurityDao {
    private Context context;
    private Dao<SafeInsResponse.SafeBean,Integer> dao;
    private DatabaseHelper helper;


    public SecurityDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            dao = helper.getDao(SafeInsResponse.SafeBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void insert(SafeInsResponse.SafeBean res){
        try {
            RceLog.d("lml","安全检查insert"+res.toString());
            dao.createOrUpdate(res);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public List<SafeInsResponse.SafeBean> getSafeCreateByEn(String userId,String enterpriseCode){
        List<SafeInsResponse.SafeBean> list = new ArrayList<>();
        try {
            QueryBuilder<SafeInsResponse.SafeBean, Integer> builder = dao.queryBuilder().orderBy("id",false);
            builder.where().eq("createUserId",userId).and().eq("enterpriseCode",enterpriseCode).and().ne("workState",1);
            list= builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return list;
        }
    }
    public List<SafeInsResponse.SafeBean> getAll(){
        List<SafeInsResponse.SafeBean> list = new ArrayList<>();
        QueryBuilder<SafeInsResponse.SafeBean, Integer> builder = dao.queryBuilder();
        try {
            list = builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public SafeInsResponse.SafeBean getSafeById(String orderid,String enterpriseCode){
        List<SafeInsResponse.SafeBean> list = new ArrayList<>();
        try {
            QueryBuilder<SafeInsResponse.SafeBean, Integer> builder = dao.queryBuilder();
            builder.where().eq("enterpriseCode",enterpriseCode).and().eq("id",orderid);
            list= builder.query();
            if (list.size()>0)return list.get(0);
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<SafeInsResponse.SafeBean> searchSafe(int current,String username ,String enterpriseCode, String title, String create, String response, String status, String date) {
        List<SafeInsResponse.SafeBean> list = new ArrayList<>();
        try {
            QueryBuilder<SafeInsResponse.SafeBean, Integer> builder = dao.queryBuilder();
            builder.where().eq("enterpriseCode",enterpriseCode);
            if (title!=null)builder.where().and().like("titleName","%"+title+"%");
            if (create!=null)builder.where().and().like("createUserName","%"+create+"%");
            if (response!=null)builder.where().and().like("responUser","%"+response+"%");
            if (status!=null)builder.where().and().eq("workState","%"+status+"%");
            if (date!=null)builder.where().and().like("createDate","%"+date+"%");
            switch (current){
                case 0:
                    builder.where().and().eq("createUserName",username);
                    break;
                case 1:
                    builder.where().and().not().eq("createUserName",username);
                    builder.where().and().not().eq("responUser",username);
                    break;
                case 2:
                    builder.where().and().eq("responUser",username);
                    break;
            }
            list= builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return list;
        }
    }

    public void del(String enterpriseCode, String s) {
        try {
            DeleteBuilder<SafeInsResponse.SafeBean, Integer> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().eq("enterpriseCode",enterpriseCode).and().eq("id",s);
            deleteBuilder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<SafeInsResponse.SafeBean> getCalendarSafe(String userId, String enterpriseCode, String date) {
        List<SafeInsResponse.SafeBean> list = new ArrayList<>();
        try {
            QueryBuilder<SafeInsResponse.SafeBean, Integer> builder = dao.queryBuilder().orderBy("id",false);
            builder.where().eq("enterpriseCode",enterpriseCode).and().eq("responUserId",userId).and().eq("createDate",date).and().ne("workState",1);
            list= builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return list;
        }
    }

    public List<SafeInsResponse.SafeBean> getSafeResponseByEn(String userId, String enterpriseCode) {
        List<SafeInsResponse.SafeBean> list = new ArrayList<>();
        try {
            QueryBuilder<SafeInsResponse.SafeBean, Integer> builder = dao.queryBuilder().orderBy("id",false);
            builder.where().eq("responUserId",userId).and().eq("enterpriseCode",enterpriseCode).and().ne("workState",1);
            list= builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return list;
        }
    }
    public List<SafeInsResponse.SafeBean> getSafePartakeByEn(String userId, String enterpriseCode) {
        List<SafeInsResponse.SafeBean> list = new ArrayList<>();
        try {
            QueryBuilder<SafeInsResponse.SafeBean, Integer> builder = dao.queryBuilder().orderBy("id",false);
            builder.where().ne("workState",1).and().eq("enterpriseCode",enterpriseCode).and().eq("wotype",3).and().eq("localUserId",userId);
            list= builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<SafeInsResponse.SafeBean> getSearch(String enterpriseCode, String title, String create, String response, String date, int currentItem) {
        List<SafeInsResponse.SafeBean> list = new ArrayList<>();
        QueryBuilder<SafeInsResponse.SafeBean, Integer> builder = dao.queryBuilder().orderBy("id",false);
        Where<SafeInsResponse.SafeBean, Integer> where = builder.where();

        try {
            where.eq("enterpriseCode",enterpriseCode);
            if (!TextUtils.isEmpty(title))where.and().like("titleName","%"+title+"%");
            if (!TextUtils.isEmpty(create))where.and().like("createUserName","%"+create+"%");
            if (!TextUtils.isEmpty(response))where.and().like("responUser","%"+response+"%");
            if (!TextUtils.isEmpty(create))where.and().like("createUserName","%"+create+"%");
            if (!TextUtils.isEmpty(date))where.and().like("createDate","%"+date+"%");
            String userId = CacheTask.getInstance().getUserId();
            switch (currentItem){
                case 0:
                    where.and().eq("createUserId",userId);
                    break;
                case 1:
                    where.and().eq("responUserId",userId);
                    break;
                case 2:
                    where.and().eq("wotype",3);
                    where.and().eq("localUserId",userId);
                    break;
            }
            list = where.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }


    public SafeInsResponse.SafeBean getSafeByDisId(String disId) {
        List<SafeInsResponse.SafeBean> list = new ArrayList<>();
        QueryBuilder<SafeInsResponse.SafeBean, Integer> builder = dao.queryBuilder();
        try {
            list = builder.where().eq("chatid", disId).query();
            if (list.size()>0){
                return list.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
